import express from "express";
import cors from "cors";
import { fileURLToPath } from "url";
import path from "path";
import "dotenv/config";
import { connectDB } from "./config/db.js";
import userRoutes from "./routes/userRoutes.js";
import resumeRoutes from "./routes/resumeRoutes.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express();
const PORT = 4000;

app.use(cors());

// 连接数据库
connectDB();

// 中间件
app.use(express.json());

app.use("/api/auth", userRoutes);
app.use("/api/resume", resumeRoutes);

app.use(
  "/uploads",
  express.static(path.join(__dirname, "upload"), {
    setHeader: (req, res) => {
      res.set("Access-Control-Allow-Origin", "http://localhost:5173/");
    },
  })
);

// 路由
app.get("/", (req, res) => {
  res.send("API is working");
});

app.listen(PORT, () =>
  console.log(`Server running on http://localhost:${PORT}`)
);
